//
//  MHCreatePacketTableViewCell.m
//  MiaoCircle
//
//  Created by 许正荣 on 2018/11/4.
//  Copyright © 2018年 acgera. All rights reserved.
//

#import "MHCreatePacketTableViewCell.h"
#import "UIImage+Extension.h"
@implementation MHCreatePacketTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.amountLabel.font = MHRegularFont_15;
    self.numberLabel.font = MHRegularFont_15;
    self.rangeLabel.font = MHRegularFont_15;
    self.amountTextFeild.font = MHRegularFont_15;
    self.numberTextFeild.font = MHRegularFont_15;
    self.rangeTextField.font = MHRegularFont_15;
    self.reMarkTextFeild.font = MHRegularFont_15;
    self.finishBtn.titleLabel.font = MHRegularFont_15;
    self.finishBtn.backgroundColor = MHColorFromHexString(@"#FE313D");
    self.finishBtn.layer.cornerRadius = 22.0;
    self.finishBtn.masksToBounds = YES;
    self.containerView.layer.cornerRadius = 8;
    self.containerView.masksToBounds = YES;
    self.avaterImageView.layer.cornerRadius  = 44;
    self.avaterImageView.layer.masksToBounds = YES;
    self.tipsLabel.font = MHRegularFont_12;
    [self.avaterImageView yy_setImageWithURL:[NSURL URLWithString:[[MHHTTPService sharedInstance]  currentUser].allHeadPath] options:nil];
//    [self.avaterImageView yy_setImageWithURL:[NSURL URLWithString:[[MHHTTPService sharedInstance]  currentUser].allHeadPath] placeholder:nil options:nil completion:^(UIImage * _Nullable image, NSURL * _Nonnull url, YYWebImageFromType from, YYWebImageStage stage, NSError * _Nullable error) {
//        UIImage *corpImage = [image imageByRoundCornerRadius:image.size.width/2 borderWidth:10 borderColor:MHBGColorD8];
//        self.avaterImageView.image = corpImage;
//    }];
    self.amountTextFeild.delegate = self;
    self.headerTipsLabel.font = MHRegularFont_15;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)string
{
    //新输入的
    if (string.length == 0) {
        return YES;
    }
    
    //第一个参数，被替换字符串的range
    //第二个参数，即将键入或者粘贴的string
    //返回的是改变过后的新str，即textfield的新的文本内容
    NSString *checkStr = [textField.text stringByReplacingCharactersInRange:range withString:string];
    
    //正则表达式（只支持两位小数）
    NSString *regex = @"^\\-?([1-9]\\d*|0)(\\.\\d{0,2})?$";
    //判断新的文本内容是否符合要求
    return [self isValid:checkStr withRegex:regex];
    
}

//检测改变过的文本是否匹配正则表达式，如果匹配表示可以键入，否则不能键入
- (BOOL) isValid:(NSString*)checkStr withRegex:(NSString*)regex
{
    NSPredicate *predicte = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
    return [predicte evaluateWithObject:checkStr];
}



@end
